/**
 * 
 */
package org.ejc.struts.login;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Category;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.ejc.api.executive.ConventionExecutiveAPILocal;
import org.ejc.api.executive.ConventionExecutiveAPIUtil;
import org.ejc.persist.model.ConventionExecutive;
import org.ejc.session.EJCSession;

/**
 * @author Brian Boyle
 * @struts.action path="/struts/login/loginAction" scope="request"
 *                name="loginForm" parameter="methodToCall"
 *                validate="false"
 *                input="/struts/login/login.jsp"
 * 
 * @struts.action-forward name="loginSuccess" path="/struts/login/home.jsp"
 * @struts.action-forward name="loginFailure" path="/struts/login/login.jsp"
 */
public class LoginAction extends DispatchAction {

	Category log = Category.getInstance(LoginAction.class);
	/**
	 * 
	 */
	public LoginAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		LoginAction action = new LoginAction();
		action.execute(null, null, null, null);

	}
	
	public ActionForward preLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	throws Exception {
		HttpSession session = request.getSession();
		log.debug("Checking if user is logged in......");
		if(session.getAttribute("ejcSession") != null){
			log.debug("User is already logged in");
			ActionMessages messages = new ActionMessages();
			ActionMessage msg = new ActionMessage("info.login.alreadyLoggedIn");
			messages.add("message1", msg);
			saveMessages(request, messages);
			return mapping.findForward("loginSuccess");
		}
		log.debug("Its a new user.......");
		return mapping.findForward("loginFailure");
	}
	

	public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	throws Exception {
		
		LoginForm loginForm = (LoginForm) form;
		ConventionExecutiveAPILocal conExecLocal = (ConventionExecutiveAPILocal)ConventionExecutiveAPIUtil.getLocalHome().create();
		ConventionExecutive executive = conExecLocal.getExecutive(loginForm.getUsername());
		
		ActionMessages errors = new ActionMessages();
		if(executive == null){
			errors.add("username", new ActionMessage("errors.login.username"));
		}else{
			if(!conExecLocal.validatePassword(loginForm.getPassword(), executive.getPassWord())){
				errors.add("password", new ActionMessage("errors.login.password"));
			}
		}
		if ( errors != null && !errors.isEmpty() ) {
			saveErrors(request, errors);
			return (mapping.findForward("loginFailure"));
		}
		
			log.fatal("Is this exec an admin? -->"+executive.isAdmin());
		
			EJCSession ejcSession = new EJCSession(executive);
			ejcSession.setCurrentAction("Welcome");
			ejcSession.setConvention(executive.getConvention());
			
			HttpSession session = request.getSession();
			Locale locale = new Locale(executive.getLanguageIso().getLanguageIso());
			request.setAttribute("Locale", locale);
			
			session.setAttribute("ejcSession", ejcSession);
			return mapping.findForward("loginSuccess");
	}

	
	public void test() {
		/*Properties p = new Properties();
		p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
		p.put(Context.PROVIDER_URL, "localhost:1099");
		InitialContext context = new javax.naming.InitialContext(p);*/	
		
		// Lookup CustomerSS EJB
		//Object object = context.lookup( "java:org.ejc/ConventionExecutiveAPIHome");
		
		/*ConventionExecutiveAPIHome conventionHome = ( ConventionExecutiveAPIHome )
			PortableRemoteObject.narrow(object, ConventionExecutiveAPIHome.class);
		conventionHome.create().getExecutive();*/
	}
}
